﻿/*
    This file is part of News Worm.

    News Worm is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    News Worm is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with News Worm.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Threading;
using System.ComponentModel;

namespace NewsWorm {
    internal partial class RecommendedFeedsDialog : Form, IPerformanceMonitor {
        internal RecommendedFeedsDialog( Point location, IMainWindowClient client) {
            InitializeComponent();

            this.listViewRecommendedFeeds.ShowGroups = true;
            this.listViewRecommendedFeeds.View = View.Details;
            this.listViewRecommendedFeeds.FullRowSelect = true;
            this.listViewRecommendedFeeds.Columns.Add( "dummy", this.Width - 20 );

            NewsWorm.NativeMethods.SetWindowTheme( this.listViewRecommendedFeeds.Handle, "explorer", null );

            this.listViewRecommendedFeeds.ShowItemToolTips = true;
            this.listViewRecommendedFeeds.CheckBoxes = true;
            this.Location = location;

            m_Client = client;
            this.checkBoxShowOnStartup.Checked = m_Client.ShowOnStartUp;
            this.Font = SystemFonts.MessageBoxFont;
        }

        private IMainWindowClient m_Client;

        private void InitView() {
            this.Cursor = Cursors.WaitCursor;
            bool FeedsAdded = true;
            ListViewGroup GlobalGroup = new ListViewGroup( "Global News", "Global News" );
            ListViewGroup SoftwareGroup = new ListViewGroup( "Software And Technology", "Software And Technology" );
            ListViewGroup FinnishGroup = new ListViewGroup( "Finland News", "Finland News" );
            ListViewGroup BlogsGroup = new ListViewGroup( "Blogs", "Blogs" );

            this.listViewRecommendedFeeds.Groups.Add( GlobalGroup );
            this.listViewRecommendedFeeds.Groups.Add( SoftwareGroup );
            this.listViewRecommendedFeeds.Groups.Add( FinnishGroup );
            this.listViewRecommendedFeeds.Groups.Add( BlogsGroup );

            m_Feeds = new List<IRecommendedFeed>();

            progressBarLoader.PerformStep();

            try {
                m_Feeds.Add( new RecommendedFeed( "http://feeds.bbci.co.uk/news/rss.xml", 0, "Global News", this,m_Client.Events) );
                m_Feeds.Add( new RecommendedFeed( "http://rss.cnn.com/rss/edition.rss", 0, "Global News", this, m_Client.Events ) );
                m_Feeds.Add( new RecommendedFeed( "http://www.nytimes.com/services/xml/rss/nyt/InternationalHome.xml", 0, "Global News", this, m_Client.Events ) );
                m_Feeds.Add( new RecommendedFeed( "http://www.theguardian.com/world/rss", 0, "Global News", this, m_Client.Events ) );
                m_Feeds.Add( new RecommendedFeed( "http://www.theregister.co.uk/software/headlines.atom", 0, "Software And Technology", this, m_Client.Events ) );
                m_Feeds.Add( new RecommendedFeed( "https://news.ycombinator.com/rss", 0, "Software And Technology", this, m_Client.Events ) );
                m_Feeds.Add( new RecommendedFeed( "http://winsupersite.com/rss.xml", 0, "Software And Technology", this, m_Client.Events ) );
                m_Feeds.Add( new RecommendedFeed( "http://feeds.gawker.com/lifehacker/full", 0, "Software And Technology", this, m_Client.Events ) );
                m_Feeds.Add( new RecommendedFeed( "http://www.drdobbs.com/rss/all", 0, "Software And Technology", this, m_Client.Events ) );
                m_Feeds.Add( new RecommendedFeed( "http://www.aamulehti.fi/rss/aamulehti_10_tuoreinta.rss", 0, "Finland News", this, m_Client.Events ) );
                m_Feeds.Add( new RecommendedFeed( "http://www.iltalehti.fi/rss.xml", 0, "Finland News", this, m_Client.Events ) );
                m_Feeds.Add( new RecommendedFeed( "http://www.hs.fi/rss/?osastot=kotimaa", 0, "Finland News", this, m_Client.Events ) );
                m_Feeds.Add( new RecommendedFeed( "http://www.f-secure.com/weblog/weblog.rss", 0, "Blogs", this, m_Client.Events ) );
                m_Feeds.Add( new RecommendedFeed( "http://blogs.msdn.com/b/oldnewthing/rss.aspx", 0, "Blogs", this, m_Client.Events ) );
            } catch ( Exception ex ) {
                Console.Write( ex.Message );
                FeedsAdded = false;
            }
            progressBarLoader.Visible = false;
            labelPleaseWait.Visible = false;

            if ( FeedsAdded ) {
                foreach ( IRecommendedFeed f in m_Feeds ) {
                    if ( f.Title != null ) {
                        ListViewItem item = new ListViewItem( f.Title, this.listViewRecommendedFeeds.Groups[f.Group] );
                        item.Name = f.Url;
                        item.ToolTipText = f.Description;
                        this.listViewRecommendedFeeds.Items.Add( item );
                    }
                }

                foreach ( Feed f in m_Client.AvailableFeeds ) {
                    foreach ( ListViewItem item in this.listViewRecommendedFeeds.Items ) {
                        if ( item.Name.Equals( f.Url ) )
                            item.Checked = true;
                    }
                }
            }
            this.Cursor = Cursors.Default;
        }

        private List<IRecommendedFeed> m_Feeds;

        private void RecommendedFeedsDialog_Shown( object sender, EventArgs e ) {
            Application.DoEvents();
            InitView();
        }

        private void buttonOK_Click( object sender, EventArgs e ) {
            this.Cursor = Cursors.WaitCursor;

            List<Feed> Temp = new List<Feed>();

            foreach ( ListViewItem item in listViewRecommendedFeeds.CheckedItems ) {
                foreach ( RecommendedFeed feed in m_Feeds ) {
                    if ( item.Name.Equals( feed.Url ) )
                        m_Client.AddRecommendedFeed( feed );
                }
            }

            foreach ( ListViewItem item in listViewRecommendedFeeds.Items ) {
                foreach ( Feed feed in m_Client.AvailableFeeds ) {
                    if ( item.Checked == false ) {
                        if ( item.Name.Equals( feed.Url ) ) {
                            //Can't modify collection while enumerating so add feeds to a temporary collection.
                            Temp.Add( feed );
                        }
                    }
                }
            }

            foreach ( Feed feed in Temp )
                m_Client.RemoveFeedIfAvailable( feed );

            this.Cursor = Cursors.Default;
            m_Client.RefreshAll();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void buttonCancel_Click( object sender, EventArgs e ) {
            this.Close();
        }

        private void checkBoxShowOnStartup_CheckedChanged( object sender, EventArgs e ) {
            if ( checkBoxShowOnStartup.Checked )
                m_Client.EnableRecommendedFeedOnStartup();
            else
                m_Client.DisableRecommendedFeedOnStartup();
        }

        void IPerformanceMonitor.GenerateProgressStep() {

            EventHandler eh = delegate {
                progressBarLoader.PerformStep();
            };
            progressBarLoader.Invoke( eh );

        }
    }
}
